package il.ac.braude.sona.entities;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;


/**
 * This class defines user entity.
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * Laboratory in Software Engineering,Ort Braude College.</p>
 *
 */
public class User implements Serializable{
	public static String[] fields =  {"fname","lname","uid","tid"};
	private HashSet<String> premissions;
	private HashMap<String,String> values = new HashMap<String,String>();
	
	/**
	 * Class constructor.
	 * @param rs defines user data.
	 * @param permissions defines user permissions.
	 * @throws SQLException
	 */
	public User(ResultSet rs, HashSet<String> permissions) throws SQLException {
		for (String f : fields) 
			values.put(f, rs.getString(f));
		this.premissions=permissions;
	}
	/**
	 * Class getter.
	 * @param key values to return.
	 * @return user parameters.
	 */
	public String getValue(String key) {
		return values.get(key);
	}
/**
 * Method that returns weather certain user has the proper permissions.
 * @param permission
 * @return true if user has permissions.
 */
	public boolean hasPremission(String permission) {
		return premissions.contains(permission);
	}
	/**
	 * Returns full user's name.
	 * @return user name.
	 */
	public String getFullName() {
		return values.get(fields[0]) +" "+values.get(fields[1]);
	}
	/**
	 * Returns user ID
	 * @return ID
	 */
	public String getID() {
		return getValue(fields[2]);
	}
	@Override
	public String toString() {
		return getID() + " : " +getFullName();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj==this) return true;
		if (obj instanceof String) return getID().equals(obj);
		if (obj instanceof User) return getID().equals(((User) obj).getID());
		return false;
	}

}
